package com.ebupt.migu.music.sms.controller;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.sms.service.SmsTmplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
@RestController
@RequestMapping("/sms/tmpl")
public class SmsTmplController {
    // 这样使用，request每次进来都是Current Request
    @Autowired
    HttpServletRequest request;
    @Autowired
    private SmsTmplService smsService;

    @RequestMapping("/add")
    @ResponseBody
    public ResultObject<Object> add() {
        ResultObject<Object> retObj = new ResultObject<>();

        // 获取用户信息和业务参数
        Map<String, Object> paramMap = AuthBaseUtils.authBaseInfo(request);

        Map<String, Object> retMap = smsService.addSmsTmpl(paramMap);

        retObj.setCode(retMap.get("code").toString());
        retObj.setMsg(retMap.get("msg").toString());
        retObj.setData(retMap.get("data"));

        return retObj;
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultObject<Object> del() {
        ResultObject<Object> retObj = new ResultObject<>();
        Map<String,Object> paramMap = ReqestParamUtil.getParams(request);

        // todo 先判断是否创建者或者超级管理员

        // todo 如果是超级管理员或者创建者则可以删除
        if (true) {
            smsService.delSmsTmpl(paramMap);
            retObj.setMsg("删除短信模板成功");
        } else {
            throw new MyException(StatusEnum.SMS_TEMPLATE_DELETE_FAIL, "只有创建者或管理员可以删除");
        }

        return retObj;
    }

    @RequestMapping("/query")
    @ResponseBody
    public ResultObject<Object> query() {
        ResultObject<Object> retObj = new ResultObject<>();

        Map<String,Object> paramMap = ReqestParamUtil.getParams(request);
        Page<Map<String, Object>> page = smsService.querySmsTmpl(paramMap);

        retObj.setData(page);
        retObj.setMsg("查询短信模板成功");

        return retObj;
    }

    @RequestMapping("/checkers")
    @ResponseBody
    public ResultObject<Object> checkers() {
        ResultObject<Object> retObj = new ResultObject<>();

        // 获取用户信息和业务参数
        Map<String, Object> paramMap = AuthBaseUtils.authBaseInfo(request);
        Page<List<Map<String, Object>>> page = smsService.queryCheckers(paramMap);

        retObj.setData(page);
        retObj.setMsg("查询成功");

        return retObj;
    }

    @RequestMapping("/validateSensitiveWords")
    @ResponseBody
    public ResultObject<Object> validateSensitiveWords() {
        ResultObject<Object> retObj = new ResultObject<>();

        Map<String,Object> paramMap = ReqestParamUtil.getParams(request);
        Map<String, Object> retMap = smsService.validateSensitiveWords(paramMap);

        retObj.setCode(retMap.get("code").toString());
        retObj.setMsg(retMap.get("msg").toString());

        return retObj;
    }
}